import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue' //@代表src目录
import Home from '@/views/Home.vue'
import UserList from "@/views/user/UserList"
import Menus from "@/views/menu/Menus";
import RoleList from "../views/role/RoleList";

Vue.use(VueRouter)

const routes = [
  {
    path: "/",redirect:"/login"
  },
  {
    path: '/login', name: '登陆', component:Login
  },
  {
    path: '/home', name: '主页', component:Home,children:[
      {
        path: '/userList', name: '用户管理', component:UserList
      },
      {
        path: '/roleList', name: '用户管理', component:RoleList
      },
      {
        path: '/menus', name: '菜单管理', component:Menus
      }

    ]
  }

]

const router = new VueRouter({
  routes
})
//to 路由目标, from 从哪里发过来的, next 拦截不拦截，拦截到那
router.beforeEach(function (to, from, next) {
  if (to.path == '/login') {
    return next();
  }
  let user = sessionStorage.getItem("user");//获取sessionStorage用户信息
  if (user != null) {
    //当前用户已经登陆
    return next();
  } else {
    return next('/login');
  }

  /*alert("to:  " + to.path);
  alert("from:" + to.path);*/

});

export default router
